jQuery(document).ready(function($) 
{
	/* 
	**
	** Global functions 
	**
	***/		
	
	//define ajaxurl
	var ajax_url = $("#ajax_url").val();	
	
	/* 
	**
	** Specific site pages behaviours
	**
	***/	
	/* Hide admin link */	
	$("#admin_link").hover(
	  function () {
		$("#hidden_link a").show();
	  }, 
	  function () {
		$("#hidden_link a").hide();
	  }
	);
	
	/* 
	**
	** Show Tifinagh on hover
	**
	***/	
	$("#alphabeth li").hover(
	  function () {
		$(this).find('span').hide();
		$(this).find('span.tz').show();
	  }, 
	  function () {
		$(this).find('span').hide();
		$(this).find('span:first').show();
	  }
	);
	
	/* 
	**
	** Insert special characters on the title field of the add new form
	**
	***/		
	$.fn.insertAtCaret = function (tagName) {
		return this.each(function(){
			if (document.selection) {
				//IE support
				this.focus();
				sel = document.selection.createRange();
				sel.text = tagName;
				this.focus();
			}else if (this.selectionStart || this.selectionStart == '0') {
				//MOZILLA/NETSCAPE support
				startPos = this.selectionStart;
				endPos = this.selectionEnd;
				scrollTop = this.scrollTop;
				this.value = this.value.substring(0, startPos) + tagName + this.value.substring(endPos,this.value.length);
				this.focus();
				this.selectionStart = startPos + tagName.length;
				this.selectionEnd = startPos + tagName.length;
				this.scrollTop = scrollTop;
			} else {
				this.value += tagName;
				this.focus();
			}
		});
	};
	
	/* 
	**
	** Update Tifinagh letters
	**
	***/
	$.fn.update_tifinagh = function ( source ) {
		return this.each(function(){
			var word = $(source).val().toLowerCase();
		
			var alphabet = {'a': 'ⴰ','b': 'ⴱ', 'g': 'ⴳ', 'gw': 'ⴳⵯ', 'd': 'ⴷ', 'ḍ': 'ⴹ', 'e': 'ⴻ', 'f': 'ⴼ', 'k': 'ⴽ', 'kw': 'ⴽⵯ', 'h': 'ⵀ', 'ḥ': 'ⵃ', 'ɛ': 'ⵄ', 'x': 'ⵅ', 'q': 'ⵇ', 'i': 'ⵉ', 'j': 'ⵊ', 'l': 'ⵍ', 'm': 'ⵎ', 'n': 'ⵏ', 'u': 'ⵓ', 'r': 'ⵔ', 'ṛ': 'ⵕ', 'ɣ': 'ⵖ', 's': 'ⵙ', 'ṣ': 'ⵚ', 'c': 'ⵛ', 't': 'ⵜ', 'ṭ': 'ⵟ', 'w': 'ⵡ', 'y': 'ⵢ', 'z': 'ⵣ', 'ẓ': 'ⵥ'};
			
			var tifinaghized = word.replace(/[abgdḍefkhḥɛxqijlmnurṛɣsṣctṭwyzẓ]|gw|kw/g, function(s) {
				return alphabet[s];
			});
			
			$("span.generated_tifinaghe").html(tifinaghized);
		});
	};
	
});/* END MAIN*/